
package sk.lieskove.jianghongtiao.aresapi.response.obj;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import sk.lieskove.jianghongtiao.common.utils.xml.ProcessXML;

import javax.persistence.*;
import javax.xml.xpath.XPathExpressionException;
import java.io.Serializable;


/**
 * Definice elementu obecné chyby dotazu v ARES
 * 
 * <p>Java class for error_ARES complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="error_ARES">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Error_kod" type="{http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_datatypes/v_1.0.4}error_kod_ARES"/>
 *         &lt;element name="Error_text" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */

@Entity
@Table(name="ares_error")
public class ErrorARES implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    protected byte errorKod;
    protected String errorText;

    @Transient
    private String xPath;
    @Transient
    private Document document;
    @Transient
    private Logger log = Logger.getLogger(ErrorARES.class);
    @OneToOne(mappedBy = "error", cascade=CascadeType.PERSIST)
    private OdpovedFo odpovedFo;

    public ErrorARES() {
    }

    private void setErrorKod(){
        String ec = getValue("Error_kod");
        if(ec != null){
            errorKod = Byte.parseByte(ec);
        } else {
            errorKod = 0;
        }
    }

    private void setErrorText(){
        errorText = getValue("Error_text");
    }

    public ErrorARES(String xPath, Document document) {
        this.xPath = xPath;
        this.document = document;
        setErrorKod();
        setErrorText();
    }

    private String getValue(String nodeName){
        String xp = xPathConstructor(nodeName);
        log.debug("Loading '"+xp+"'");
        try {
            Node output = ProcessXML.getNodeByXPathQuery(xp, document);
            if (output == null) {
                log.warn("Cannot load: "+xp);
            } else {
                return output.getTextContent();
            }
        } catch (XPathExpressionException ex) {
            log.warn("Cannot load '"+xp+"' property. ",ex);
        }
        return null;
    }

    private String xPathConstructor(String subNode){
        return xPath + "/" + subNode;
    }

    /**
     * Gets the value of the errorKod property.
     * 
     */
    public byte getErrorKod() {
        return errorKod;
    }

    /**
     * Sets the value of the errorKod property.
     * 
     */
    public void setErrorKod(byte value) {
        this.errorKod = value;
    }

    /**
     * Gets the value of the errorText property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getErrorText() {
        return errorText;
    }

    /**
     * Sets the value of the errorText property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setErrorText(String value) {
        this.errorText = value;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public OdpovedFo getOdpovedFo() {
        return odpovedFo;
    }

    public void setOdpovedFo(OdpovedFo odpovedFo) {
        this.odpovedFo = odpovedFo;
    }

}
